1
|
|
|
|
2
|
|
|
UB.wordSeperators = [" ", ".", ",", ";", "[", "]", "{", "}", "(", ")"]; |
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
var stringFuncs = { |
6
|
|
|
|
7
|
|
|
/** advanced: splits by any operator or number char, space & newline. |
8
|
|
|
* simple: splits by newline & space. |
9
|
|
|
* always returns an array of non-blank, trimmed words. */ |
10
|
|
|
splitWords: function(advanced = true){ |
11
|
|
|
var text = this; |
12
|
|
|
|
13
|
|
|
if (advanced){ |
14
|
|
|
|
15
|
|
|
var results = []; |
16
|
|
|
|
17
|
|
|
// find start of word |
18
|
|
|
for (var c = 0, cl = text.length;c<cl;c++){ |
19
|
|
|
var cc = text.charCodeAt(c); |
20
|
|
|
var start = c; |
21
|
|
|
|
22
|
|
|
if (Chars.IsLetter(cc)) { |
|
|
|
|
23
|
|
|
|
24
|
|
|
// find end of word |
25
|
|
|
for (var e = c + 1;e<cl;e++){ |
26
|
|
|
var ec = text.charCodeAt(e); |
27
|
|
|
var isLastChar = (e == (cl - 1)); |
28
|
|
|
|
29
|
|
|
if ((!Chars.IsLetter(ec) && ec != charCodes.Space) || isLastChar){ |
|
|
|
|
30
|
|
|
|
31
|
|
|
// skip if "letter [dash] letter" |
32
|
|
|
if (!isLastChar && ec == charCodes.Minus && Chars.IsLetter(text.charCodeAt(e + 1))){ |
33
|
|
|
continue; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// add word if something found |
37
|
|
|
var end = isLastChar?e:e-1; |
38
|
|
|
if (end > start) { |
39
|
|
|
|
40
|
|
|
var term = text.getRange(start, end).trim(); |
41
|
|
|
if (S.Exists(term)){ |
|
|
|
|
42
|
|
|
|
43
|
|
|
if (splitBySpaceAlso) { |
|
|
|
|
44
|
|
|
A.Add(results, S.SplitWords(term)); |
|
|
|
|
45
|
|
|
} else { |
46
|
|
|
results.push(term); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// continue at ending |
52
|
|
|
c = e; |
|
|
|
|
53
|
|
|
break; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
return results; |
59
|
|
|
|
60
|
|
|
}else{ |
|
|
|
|
61
|
|
|
|
62
|
|
|
// SPLIT BY NEWLINE & SPACES |
63
|
|
|
var results = []; |
|
|
|
|
64
|
|
|
var lines = text.splitLines(true, true); |
65
|
|
|
for (var s = 0, sl = lines.length; s < sl; s++) { |
66
|
|
|
var line = lines[s]; |
67
|
|
|
var results = line.split(" "); |
|
|
|
|
68
|
|
|
for (var w = 0, wl = results.length; w < wl; w++) { |
69
|
|
|
var word = results[w]; |
70
|
|
|
if (word.length > 0) { |
71
|
|
|
results.push(word); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
return results; |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
}, |
79
|
|
|
|
80
|
|
|
joinWords: function(){ |
81
|
|
|
var words = this; |
82
|
|
|
return words.join(" "); |
83
|
|
|
}, |
84
|
|
|
|
85
|
|
|
/** Count words in a string */ |
86
|
|
|
wordCount: function(){ |
87
|
|
|
var text = this; |
88
|
|
|
return text.match(new RegExp("\\b\\w+\\b", "g")).length; |
89
|
|
|
}, |
90
|
|
|
none:null |
91
|
|
|
}; |
92
|
|
|
|
93
|
|
|
// register funcs |
94
|
|
|
UB.registerFuncs(String.prototype, stringFuncs); |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.